package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.CommVLayoutAdapter;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.PostInfo;
import com.chexiehui.jc.listener.IHomeRefreshFragment;
import com.chexiehui.jc.mvp.home.HomeSlideNewCarCovenant;
import com.chexiehui.jc.mvp.home.HomeSlideNewCarPresenter;
import com.chexiehui.jc.widget.PengYouQuanFlagLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeSlideNewCarFragment extends BaseMvpFragment<HomeSlideNewCarPresenter> implements HomeSlideNewCarCovenant.View, IHomeRefreshFragment {

    @BindView(R.id.recycler)
    RecyclerView mRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private DelegateAdapter listAdapter;
    private CommVLayoutAdapter<CarInfo> carAdapter;
    private CommVLayoutAdapter<String> carMoreAdapter;
    private CommVLayoutAdapter<PostInfo> postAdapter;
    private BasePage<PostInfo> page;
    private int pagePosition;

    public static HomeSlideNewCarFragment getInstance(int position) {
        HomeSlideNewCarFragment fragment = new HomeSlideNewCarFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void beforeSetView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            pagePosition = bundle.getInt("position");
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        refreshLayout.setEnableScrollContentWhenRefreshed(true);
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            if (page != null && page.getTotalRecordCount() > postAdapter.getData().size()) {
                mvpPresenter.getPostList(page.getPageIndex() + 1);
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        });
        VirtualLayoutManager virtualLayoutManager = new VirtualLayoutManager(mActivity);
        mRecycler.setLayoutManager(virtualLayoutManager);
        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        mRecycler.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 20);
        listAdapter = new DelegateAdapter(virtualLayoutManager, false);
        mRecycler.setAdapter(listAdapter);

        int dp_10 = SizeUtils.dp2px(10);
        LinearLayoutHelper carHelper = new LinearLayoutHelper(0);
        carAdapter = new CommVLayoutAdapter<CarInfo>(mActivity, new ArrayList<>(),
                R.layout.item_home_slide_new_car, carHelper) {
            @Override
            public void convert(ViewHolder holder, int position, CarInfo item) {

            }
        };
        listAdapter.addAdapter(carAdapter);
        LinearLayoutHelper carMoreHelper = new LinearLayoutHelper(0);
        carMoreAdapter = new CommVLayoutAdapter<String>(mActivity, new ArrayList<>(),
                R.layout.item_look_more, carMoreHelper) {
            @Override
            public void convert(ViewHolder holder, int position, String item) {

            }
        };
        listAdapter.addAdapter(carMoreAdapter);

        LinearLayoutHelper postHelper = new LinearLayoutHelper(dp_10);
        postHelper.setMarginTop(dp_10);
        postAdapter = new CommVLayoutAdapter<PostInfo>(mActivity, new ArrayList<>(),
                R.layout.item_home_slide_recommend_post, postHelper) {
            @Override
            public void convert(ViewHolder holder, int position, PostInfo item) {
                PengYouQuanFlagLayout pyq_image_box = holder.getView(R.id.pyq_image_box);
                List<String> l = new ArrayList<>();
                l.add("http://img3.imgtn.bdimg.com/it/u=2654391087,617464290&fm=26&gp=0.jpg");
                l.add("http://img2.imgtn.bdimg.com/it/u=223852992,1065328538&fm=26&gp=0.jpg");
                l.add("http://img3.imgtn.bdimg.com/it/u=1982720944,3561273177&fm=26&gp=0.jpg");
                pyq_image_box.setImgList(l);
            }
        };
        listAdapter.addAdapter(postAdapter);

        mvpPresenter.getCarList();
        mvpPresenter.getPostList(1);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.include_refresh;
    }

    @Override
    protected HomeSlideNewCarPresenter createPresenter() {
        return new HomeSlideNewCarPresenter(this);
    }

    @Override
    public void onGetCarListSuccess(BaseModel<BasePage<CarInfo>> successResult) {
        if (successResult.getData().getTotalRecordCount() > successResult.getData().getPageSize()) {
            carMoreAdapter.clearData();
            carMoreAdapter.getData().add("查看更多");
            carMoreAdapter.notifyDataSetChanged();
        } else {
            carMoreAdapter.clearData();
            carMoreAdapter.notifyDataSetChanged();
        }
        carAdapter.getData().clear();
        carAdapter.getData().addAll(successResult.getData().getItems());
        carAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetCarListFailure(BaseModel<Object> failureResult) {
        carMoreAdapter.clearData();
        carMoreAdapter.notifyDataSetChanged();
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            carMoreAdapter.getData().clear();
            carMoreAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetPostListSuccess(BaseModel<BasePage<PostInfo>> successResult) {
        page = successResult.getData();
        if (refreshLayout.getState() == RefreshState.Loading) {
            postAdapter.getData().addAll(page.getItems());
            postAdapter.notifyDataSetChanged();
            if (page.getTotalRecordCount() <= postAdapter.getData().size()) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            postAdapter.getData().clear();
            postAdapter.getData().addAll(successResult.getData().getItems());
            postAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetPostListFailure(BaseModel<Object> failureResult) {
        if (Constants.PAGE_DATA_EMPTY == failureResult.getResultCode()) {
            if (refreshLayout.getState() == RefreshState.Loading) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        } else {
            if (refreshLayout.getState() == RefreshState.Loading) {
                refreshLayout.finishLoadMore(false);
            }
        }
    }

    @Override
    public void onCallRefresh() {
        mvpPresenter.getCarList();
        mvpPresenter.getPostList(1);
    }
}
